package com.zxl.live.screen.call.ui.receiver;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;

import com.zxl.live.screen.call.helper.CallFlashHelper;
import com.zxl.live.screen.screen.constant.ScreenSetting;
import com.zxl.live.tools.common.ApplicationHelper;

/**
 * Created by Administrator on 2017/12/11.
 */

public class CallReceiver extends BroadcastReceiver {

    private static CallFlashHelper mCallFlashHelper = new CallFlashHelper();

    @Override
    public void onReceive(Context context, Intent intent) {
        if ("com.play.screen.livescreen.fake.call".equals(intent.getAction())) {
            if (ApplicationHelper.DEBUG) {
                intent.putExtra("incoming_number", "15918638337");
                mCallFlashHelper.onCallIn(intent);
            }
        } else {
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Service.TELEPHONY_SERVICE);
            int callState = tm.getCallState();
            switch (callState) {
                case TelephonyManager.CALL_STATE_RINGING:       // 电话进来时
                    if (ScreenSetting.sCallFlashSwitch) {
                        mCallFlashHelper.onCallIn(intent);
                    }
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:       // 拨打电话或者接听电话时
                    break;
                case TelephonyManager.CALL_STATE_IDLE:          // 挂起电话时候，或者没有任何反映
                    if (ScreenSetting.sCallFlashSwitch) {
                        mCallFlashHelper.onCallEnd();
                    }
                    break;
            }
        }
    }

    public static void sendCall() {
        ApplicationHelper.getContext().sendBroadcast(new Intent("com.play.screen.livescreen.fake.call"));
    }

}
